/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rw.table.render;

import com.rw.table.TableInfo;
import com.rw.table.formatter.ICellFormatter;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.*;

/**
 *
 * @author Rene Tapia
 */
public class EditByClickCellRenderer extends ACellRenderer {

    private JPanel panel;
    private JLabel label;
    private JButton button;

    public EditByClickCellRenderer() {
        this.panel = new JPanel();
        this.panel.setLayout(new BorderLayout());

        this.label = new JLabel();
        this.label.setOpaque(true);
        Dimension labelSize = new Dimension(2000, 16);
        this.label.setMaximumSize(labelSize);
        this.label.setMinimumSize(labelSize);
        this.label.setPreferredSize(labelSize);

        this.button = new JButton("...");
        this.button.setOpaque(true);
        Dimension buttonSize = new Dimension(25, 19);
        this.button.setMaximumSize(buttonSize);
        this.button.setMinimumSize(buttonSize);
        this.button.setPreferredSize(buttonSize);

        this.panel.add(label, BorderLayout.CENTER);
        this.panel.add(button, BorderLayout.EAST);
    }

    @Override
    public Component getComponent(TableInfo info) {
        JTable table = info.getTable();
        Object value = info.getValue();
        ICellFormatter formatter = info.getFormatter();
        boolean selected = info.isSelected();

        String strValue = value != null ? formatter.format(value) : "";
        this.label.setText(strValue);

        int align = formatter.getAlign();
        this.label.setHorizontalAlignment(align);

        Color color = getColor(info);
        this.label.setBackground(color);

        return panel;
    }

    @Override
    public boolean isEditableByClick() {
        return true;
    }
}
